package com.darren.euc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.ManagerDao;
import com.darren.euc.entity.Manager;
import com.darren.euc.entity.PageBean;
import com.darren.euc.service.ManagerService;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;


@Service
public class ManagerServiceImpl  implements ManagerService {
	
	@Autowired
    private ManagerDao managerDao;
	
    @Override
    public Manager save(Manager manager) {    	
        return managerDao.save(manager);
    }
    
    public Manager update(Manager manager){
    	return managerDao.update(manager);
    }
    
    @Override
    public Manager getManagerByRoleIdAndUserId(String roleId,String userId) {    	
        return managerDao.getManagerByRoleIdAndUserId(roleId, userId);
    }
    
    @Override
    public Manager findManagerById(Integer id){
    	return managerDao.findById(id);
    }
    
    @Override
	public void deleteById(Integer id) {
		Manager manager = new Manager();
		manager.setId(id);
		managerDao.delete(manager);		
	}
    
	@Override
	public List<Manager> getAll() {
		return managerDao.getAll();
	}

	@Override
	public PageBean queryForPageUseHQLByRoleId(JSONObject requestJson,Integer pageSize, Integer currentPage) {
		String roleId = requestJson.getString("roleId");
		String HQL = " from Manager where roleId = ? ";
		if(StringUtil.isEmptyOrWhitespace(roleId)){
			HQL = " from Manager ";
			return managerDao.queryForPageByHQL(HQL, pageSize, currentPage);
		}
		return managerDao.queryForPageByHQL(HQL, pageSize, currentPage,roleId);
	}
	
	@Override
	public PageBean queryForPageUseSQLByRoleName(JSONObject requestJson,Integer pageSize, Integer currentPage) {
		String roleName = requestJson.getString("roleName");
		String SQL = " select * from euc_user_role where role_id in (select id from euc_roles where role_name = ? or role_type = ?) order by create_time desc";
		if(StringUtil.isEmptyOrWhitespace(roleName)){
			SQL = " select * from euc_user_role  order by create_time desc";
			return managerDao.queryForPageBySQL(SQL, pageSize, currentPage);
		}
		return managerDao.queryForPageBySQL(SQL, pageSize, currentPage,roleName,roleName);
	}
	
}